﻿using System;
using System.Collections;
using System.Collections.Generic;
using FairyGUI;

/// <summary>
/// 战绩
/// </summary>
public class UIHome_BattleReport : UIPopupWindow {

	class SingleReport {
		public class Player {
			public int uid;
			public string name;
			public int score;
			public string headpic;

			public Player(HashtableEx player) {
				uid = player.GetInt("uid");
				name = player.GetString("name");
				score = player.GetInt("score");
				headpic = player.GetString("headpic");
			}
		}

		public int roomId;
		public string time;
		public int round;

		public List<Player> players;

		public SingleReport(HashtableEx report) {
			roomId = report.GetInt("roomId");
			time = report.GetString("time");
			round = report.GetInt("round");

			players = new List<Player>();

			HashtableEx rPlayers = report.GetMapEx("players");
			foreach(string index in rPlayers.keys) {
				players.Add(new Player(rPlayers.GetMapEx(index)));
			}
		}

		public Player mainPlayer() {
			foreach(Player player in players) {
				if (player.uid == DataKernel.inst.userInfo.uid) {
					return player;
				}
			}
			return null;
		}
	}

	class UISingleReport {
		SingleReport _report;
		GComponent _uiComp;
		public GComponent uiComp { get { return _uiComp; } }

		GButton _collBtn;

		public UISingleReport(SingleReport report, Action<UISingleReport> callback) {
			_report = report;
			_uiComp = UIPackage.CreateObject("主界面", "战绩单条").asCom;

			Transition expandTransition = _uiComp.GetTransition("收起");

			// 上
			GComponent upperComp = _uiComp.GetChild("上").asCom;

			GTextField roomId = upperComp.GetChild("房间号").asTextField;
			roomId.text = "房间号：" + report.roomId;

			GTextField time = upperComp.GetChild("时间").asTextField;
			time.text = report.time;

			GTextField round = upperComp.GetChild("局数").asTextField;
			round.text = "局数：" + report.round;

			UIHeadpicImage _headPic = new UIHeadpicImage(upperComp.GetChild("头像1").asCom);
			_headPic.SetHeadpic(report.mainPlayer().headpic);

			GTextField score = upperComp.GetChild("积分1").asTextField;
			score.text = "积分：" + report.mainPlayer().score;

			GImage winImg = upperComp.GetChild("胜").asImage;
			GImage loseImg = upperComp.GetChild("败").asImage;
			if (report.mainPlayer().score > 0) {
				loseImg.visible = false;
			} else {
				winImg.visible = false;
			}

			GButton expandBtn = upperComp.GetChild("展开按钮").asButton;
			expandBtn.onClick.Add(()=>{
				expandTransition.PlayReverse();
				expandBtn.visible = expandBtn.enabled = false;
				_uiComp.height = 227;

				callback(this);
			});

			// 下
			GComponent downComp = _uiComp.GetChild("下").asCom;

			for (int i = 1; i <= 4; ++i) {
				GComponent com1 = downComp.GetChild("头像" + i).asCom;
				GTextField com2 = downComp.GetChild("名字" + i).asTextField;
				GTextField com3 = downComp.GetChild("积分" + i).asTextField;
				com1.visible = com2.visible = com3.visible = false;
			}

			int index = 1;
			foreach(SingleReport.Player player in report.players) {
				if (player.uid == DataKernel.inst.userInfo.uid) {
					continue;
				}

				GComponent com11 = downComp.GetChild("头像" + (index)).asCom;
				UIHeadpicImage com1 = new UIHeadpicImage(com11);
				com1.SetHeadpic(player.headpic);
				GTextField com2 = downComp.GetChild("名字" + (index)).asTextField;
				com2.text = player.name;
				GTextField com3 = downComp.GetChild("积分" + (index)).asTextField;
				com3.text = "积分：" + player.score;

				com11.visible = com2.visible = com3.visible = true;

				index += 1;
			}

			_collBtn = downComp.GetChild("收起按钮").asButton;
			_collBtn.onClick.Add(()=>{
				expandTransition.Play();
				expandBtn.visible = expandBtn.enabled = true;
				_uiComp.height = 126;
			});
				
			expandTransition.Play();
			_uiComp.height = 126;
		}

		public void Coll() {
			_collBtn.onClick.Call();
		}
	}

	GComponent _detailsLayer;
	UISingleReport _curTop;
	UISingleReport _curExpand;
	float _curPosY;

	public UIHome_BattleReport(IOMessage message)
    {
        // 加载界面
        base.Init(UIPackage.CreateObject("主界面", "战绩界面").asCom);

        // 初始化关闭按钮
        GButton closeBtn = uiComp.GetChild("关闭按钮").asButton;
        closeBtn.onClick.Add(() => {
			DebugInfo.Log("关闭 战绩界面");

            AudioController.inst.PlayUIClickEffect();
            uiComp.RemoveFromParent();
            uiComp.Dispose();
            base.Close();
        });

		_detailsLayer = uiComp.GetChild ("资料").asCom;
		_curPosY = 0;
		_curTop = _curExpand = null;

		ArrayList reports = message.args.GetList ("reports");
		foreach(object report in reports) {
			SingleReport sReport = new SingleReport (new HashtableEx((Hashtable)report));
			AddReport (sReport);
		}
    }

	void AddReport(SingleReport report) {
		UISingleReport sr = new UISingleReport (report, expand => {
			if (_curExpand != null && _curExpand != expand) {
				_curExpand.Coll();
			}
			_curExpand = expand;
		});
		sr.uiComp.SetPosition(0, _curPosY, 0); _curPosY += sr.uiComp.actualHeight;
		if (_curTop != null) {
			sr.uiComp.relations.Add (_curTop.uiComp, RelationType.Top_Bottom);
		}
		_curTop = sr;
		_detailsLayer.AddChild (sr.uiComp);
	}

    /// <summary>
    /// 弹出窗口
    /// </summary>
	public static void Popup(IOMessage message)
    {
		DebugInfo.Log("打开 战绩界面");
		UIRootController.inst.PopupWindow(new UIHome_BattleReport(message));
    }
}
